﻿using DotNetExtensions.Core.Helpers;
using DotNetExtensions.Core.IOC;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DotNetExtensions.Core.MVC
{
    [AutoInject]
    public class AppSettingLoader<TSetting> where TSetting : class, new()
    {
        public AppSettingLoader()
        {
            string filename = "appsettings.json";
#if DEBUG
            filename = "appsettings.Development.json";
#endif
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
            if (File.Exists(path))
            {
                string json = File.ReadAllText(path);

                AppSettings = JsonConvert.DeserializeObject<TSetting>(json);
            }
        }
        public TSetting AppSettings { get; set; }
    }

    public class AppSettingDefault<T> : Singleton<AppSettingDefault<T>> where T : class, new()
    {
        public T AppSetings { get; set; }
        private AppSettingDefault()
        {
            string filename = "appsettings.json";
#if DEBUG
            filename = "appsettings.Development.json";
#endif
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
            if (File.Exists(path))
            {
                string json = File.ReadAllText(path);

                AppSetings = JsonConvert.DeserializeObject<T>(json);
            }
        }
    }
    
}
